package com.zmn.oms.api.controller.open;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.http.HttpUtil;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.utils.SignUtil;
import com.zmn.oms.model.dto.order.open.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * 类描述：订单接口
 *
 * @author liuying
 * @date 2019/02/16 17:35
 */
@RestController
@RequestMapping("/v1/test")
@Slf4j
public class OrderV1TestController extends OrderBaseController {

    private static final String TAG = "oms-open-api";
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    // private String url = "http://dev-api-oms.xiujiadian.com:10243";
    // private String url = "https://test-api-oms.xiujiadian.com";
    private String url = "http://local-api-oms.xiujiadian.com:10243";
    private String secret = "c5f79d384b8024d5adddb872f9651f38";
    private String appId = "10083";
    private String version = "1";

    /**
     * 查询预约时间
     *
     * @return
     */
    @RequestMapping("/queryAvailableTimeslots")
    public String queryAvailableTimes() {

        //http://api.oms.xiujiadian.com:10243/v1/test/queryAvailableTimeslots

        AvailableTimeslotsQuery availableTimeslotsQuery = new AvailableTimeslotsQuery();
        availableTimeslotsQuery.setCityId(110100);
        availableTimeslotsQuery.setLatitude(40.014708D);
        availableTimeslotsQuery.setLongitude(116.358663D);
        availableTimeslotsQuery.setProductCount(1);
        availableTimeslotsQuery.setProductId(10271);

        Map<String, String> params = Maps.newHashMap();
        params.put("appId", appId);
        params.put("jsonData", JSON.toJSONString(availableTimeslotsQuery));
        params.put("version", version);
        params.put("timestamp", "" + (System.currentTimeMillis() / 1000));
        params.put("sign", SignUtil.signMd5(params, secret));
        logger.info("{}", params);
        return HttpUtil.post(url + "/v1/queryAvailableTimeslots", params);
    }

    /**
     * 查询预约时间
     *
     * @return
     */
    @RequestMapping("/createOrder")
    public String createOrder() {

        //http://api.oms.xiujiadian.com:10243/v1/test/createOrder

        CreateOrderDTO createOrderDTO = new CreateOrderDTO();
        createOrderDTO.setContactName("联系人");
        createOrderDTO.setTelephone("15810520852");
        createOrderDTO.setGender(1);
        createOrderDTO.setCityId(110100);
        createOrderDTO.setAddress("test");
        createOrderDTO.setStreet("testStreet");
        createOrderDTO.setLatitude(40.014708D);
        createOrderDTO.setLongitude(116.358663D);
//        createOrderDTO.setLatitude(31.25121121322262);
//        createOrderDTO.setLongitude(121.49102172615575);

        createOrderDTO.setDutyTime(DateUtil.addDays(new Date(), 1));
        createOrderDTO.setRemark("备注");

        createOrderDTO.setProductCount(1);
        createOrderDTO.setProductId(10010);
        createOrderDTO.setChannelPrepayAmount(1000);

        OpenOrderDiscountDTO orderDiscount = new OpenOrderDiscountDTO();
        orderDiscount.setAmount(1000);
        orderDiscount.setRemark("测试渠道优惠");
        orderDiscount.setSourceData("{\"code\":\"xxxxx\"}");
        createOrderDTO.setOrderDiscount(orderDiscount);

        Map<String, String> params = Maps.newHashMap();
        params.put("appId", appId);
        params.put("jsonData", JSON.toJSONString(createOrderDTO));
        params.put("version", version);
        params.put("timestamp", "" + (System.currentTimeMillis() / 1000));
        params.put("sign", SignUtil.signMd5(params, secret));

        logger.info("{}", params);

        String result = HttpUtil.post(url + "/v1/createOrder", params);
        logger.info("{}", result);
        return result;
    }

    /**
     * 查询预约时间
     *
     * @return
     */
    @RequestMapping("/payOrder")
    public String payOrder(Long orderId, Integer payAmount) {

        //http://api.oms.xiujiadian.com:10243/v1/test/payOrder?orderId=355773653398413312&payAmount=300&payTime=2019-03-25 20:23:18
        //http://local-api-oms.xiujiadian.com:10243/v1/test/payOrder?orderId=355773653398413312&payAmount=1000

        PayOrderDTO payOrderDTO = new PayOrderDTO();
        payOrderDTO.setOrderId(orderId);
        payOrderDTO.setPayAmount(payAmount);
        payOrderDTO.setPayTime(new Date());
        payOrderDTO.setRemark("用户支付：" + payAmount);

        Map<String, String> params = Maps.newHashMap();
        params.put("appId", appId);
        params.put("jsonData", JSON.toJSONString(payOrderDTO));
        params.put("version", version);
        params.put("timestamp", "" + (System.currentTimeMillis() / 1000));
        params.put("sign", SignUtil.signMd5(params, secret));

        logger.info("{}", params);

        return HttpUtil.post(url + "/v1/payOrder", params);
    }

    /**
     * 查询预约时间
     *
     * @return
     */
    @RequestMapping("/cancelOrder")
    public String cancelOrder(Long orderId) {

        //http://api.oms.xiujiadian.com:10243/v1/test/payOrder?orderId=317033943688163328

        CancelOrderDTO cancelOrderDTO = new CancelOrderDTO();
        cancelOrderDTO.setOrderId(orderId);
        cancelOrderDTO.setRemark("我就取消怎么了");

        Map<String, String> params = Maps.newHashMap();
        params.put("appId", appId);
        params.put("jsonData", JSON.toJSONString(cancelOrderDTO));
        params.put("version", version);
        params.put("timestamp", "" + (System.currentTimeMillis() / 1000));
        params.put("sign", SignUtil.signMd5(params, secret));

        logger.info("{}", params);

        return HttpUtil.post(url + "/v1/cancelOrder", params);
    }

    /**
     * 查询预约时间
     *
     * @return
     */
    @RequestMapping("/queryOrder")
    public String queryOrder(Long orderId) {

        //http://api.oms.xiujiadian.com:10243/v1/test/queryOrder?orderId=317033943688163328

        OrderQueryDTO queryDTO = new OrderQueryDTO();
        queryDTO.setOrderId(orderId);

        Map<String, String> params = Maps.newHashMap();
        params.put("appId", appId);
        params.put("jsonData", JSON.toJSONString(queryDTO));
        params.put("version", version);
        params.put("timestamp", "" + (System.currentTimeMillis() / 1000));
        params.put("sign", SignUtil.signMd5(params, secret));

        logger.info("{}", params);

        return HttpUtil.post(url + "/v1/queryOrder", params);
    }

    /**
     * 测试推送状态
     *
     * @return
     */
    @RequestMapping("/orderChange")
    public ResultDTO orderChange(RequestDTO requestDTO) {
        logger.info("{}", requestDTO);
        return ResultDTO.success(requestDTO);
    }

}
